﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace teach
{
    public class English : Question, ITextQuestion
    {
        public string QuestionText {get;set;}
        public bool EnglishMode { get; set; }

        public English()
        {
            m_message = "Traduction";
        }

        public override object Answer
        {
            get
            {
                if (EnglishMode)
                {
                    return Properties["english"];
                }
                else
                {
                    return Properties["french"];
                }
            }
        }

        public override bool CheckAnswer(object userAnswer)
        {
            if (QuestionText[QuestionText.Length - 1] == '?')
            {
                if (userAnswer.ToString().Trim()[userAnswer.ToString().Trim().Length - 1] != '?')
                {
                    userAnswer = userAnswer.ToString().Trim() + "?";
                }
            }
            bool result = (userAnswer.ToString().Trim().ToLower() == Answer.ToString().ToLower());
            if (result == false)
            {
                if (EnglishMode)
                {
                    if (Properties.ContainsKey("english2"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["english2"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("english3"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["english3"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("english4"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["english4"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("english5"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["english5"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("english6"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["english6"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("english7"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["english7"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("english8"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["english8"].ToString().ToLower());
                    }
                }
                else
                {
                    if (Properties.ContainsKey("french2"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["french2"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("french3"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["french3"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("french4"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["french4"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("french5"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["french5"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("french6"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["french6"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("french7"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["french7"].ToString().ToLower());
                    }
                    if (result == false && Properties.ContainsKey("french8"))
                    {
                        result = (userAnswer.ToString().Trim().ToLower() == Properties["french8"].ToString().ToLower());
                    }
                }
            }
            return result;
        }

        public override void GenerateValues(Preference pref)
        {
            try
            {
                this.Timeout = pref.Timeout * 1000;
                Random rnd = new Random();
                if (rnd.Next(100) > 50)
                {
                    EnglishMode = true;
                    m_message = "Translation";
                    QuestionText = Properties["french"];
                }
                else
                {
                    EnglishMode = false;
                    m_message = "Traduction";
                    QuestionText = Properties["english"];
                }
            }
            catch (Exception ex)
            {
                TeachController.LogError("English.cs - GenerateValues()",Properties, ex);
                throw ex;
            }
        }
    }
}
